﻿using CPPEI.Coolzon.Common;
using CPPEI.Coolzon.Configuration;
using System;
using System.Collections.Generic;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Description;
using System.Text;

namespace CPPEI.Coolzon.UpdateServer
{
    class Program
    {
        private static ServiceHost s_updateServiceHost;

        static void Main(string[] args)
        {
            Logger.InitByLogDirectory(CZEnvironment.GetLogDirectory());
            // 初始化配置信息
            XConfigUtil.Init(CZEnvironment.Intance, CZEnvironment.GetEnvironmentConfigFilePath());

            HostUpdateService();
            InputLoop();
            StopUpdateService();

            Logger.InfoToTag("UpdateServer", "服务器关闭");
        }

        private static void HostUpdateService()
        {
            Logger.InfoToTag("UpdateServer", "更新服务启动中...");
            Configuration.Server.ServiceConfig serviceConfig = CZEnvironment.Intance.ServiceConfig;
            Uri baseAddress = new Uri(string.Format("net.tcp://{0}:{1}/{2}/", serviceConfig.IP, serviceConfig.Port, serviceConfig.Name));

            s_updateServiceHost = new ServiceHost(typeof(UpdateService), baseAddress);
            Binding binding = new NetTcpBinding();
            (binding as NetTcpBinding).Security.Mode = SecurityMode.None;
            ServiceMetadataBehavior serviceMetadataBehavior = new ServiceMetadataBehavior();
            s_updateServiceHost.Description.Behaviors.Add(serviceMetadataBehavior);

            if (serviceConfig.MetadataGet)
            {
                Logger.InfoToTag("UpdateServer", "更新服务元数据获取开关:开启");
                Logger.InfoToTag("UpdateServer", "更新服务元数据获取地址:{0}mex", baseAddress);
                s_updateServiceHost.AddServiceEndpoint(typeof(IMetadataExchange), MetadataExchangeBindings.CreateMexTcpBinding(), "mex");
            }
            else
            {
                Logger.InfoToTag("UpdateServer", "发布服务元数据获取开关:关闭");
            }

            s_updateServiceHost.AddServiceEndpoint(typeof(IUpdateService), binding, baseAddress);
            Logger.InfoToTag("UpdateServer", "更新服务地址:{0}", baseAddress);

            try
            {
                s_updateServiceHost.Open();
                Logger.InfoToTag("UpdateServer", "更新服务启动成功");
            }
            catch (Exception ex)
            {
                Logger.ErrorToTag("UpdateServer", string.Format("更新服务启动失败{0}{1}", Environment.NewLine, ex));
            }
        }

        private static void StopUpdateService()
        {
            Logger.InfoToTag("UpdateServer", "更新服务器关闭中...");
            try
            {
                s_updateServiceHost.Close();
                Logger.InfoToTag("UpdateServer", "更新服务关闭成功");
            }
            catch (Exception ex)
            {
                Logger.ErrorToTag("UpdateServer", string.Format("更新服务关闭失败{0}{1}", Environment.NewLine, ex));
            }
        }

        private static void InputLoop()
        {
            string cmd = Console.ReadLine().ToUpper();
            while (!ConsoleCMD.Execute(cmd))
            {
                cmd = Console.ReadLine().ToUpper();
            }
        }
    }
}
